package com.e3.sysmg.security.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.sysmg.security.model.UserModel;
import com.e3.sysmg.security.model.UserStatus;
import com.e3.template.tgf.GFHelper;
import com.e3.web.core.model.ModelHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.e3.web.widget.client.MyGridHelper;
import com.e3.web.widget.model.ListTypeModel;
import com.extjs.gxt.ui.client.data.ModelComparer;
import com.extjs.gxt.ui.client.data.ModelKeyProvider;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.google.gwt.i18n.client.DateTimeFormat;

public class UserHelper extends GFHelper<UserModel>{
	
	public static UserHelper INSTANCE = new UserHelper();
	
	private ModelHelper<UserModel> modelHelper = new ModelHelper<UserModel>();
	
	private String[] notNullFields = {UserModel.CODE,UserModel.PASSWORD};
	
	private DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");
	
	public ModelComparer<UserModel> getModelComparer(){
		return modelHelper.getModelComparer();
	}
	
	public ModelKeyProvider<UserModel> getModelKeyProvider(){
		return modelHelper.getModelKeyProvider();
	}

	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		if(name.equals(UserModel.CODE)){
			formField = MyFormFieldFactory.createStringField(name,dispName,20,isNotNull);
		}
		if(name.equals(UserModel.NAME)){
			formField = MyFormFieldFactory.createStringField(name,dispName,50,isNotNull);
		}
		else if(name.equals(UserModel.PASSWORD)){
			formField = MyFormFieldFactory.createPasswordField(name,dispName,100,isNotNull);
		}
		else if(name.equals(UserModel.START_DATE)||name.equals(UserModel.END_DATE)){
			formField = MyFormFieldFactory.createDateField(name,dispName,isNotNull);
		}
		else if(name.equals(UserModel.STATUS)){
			formField = MyFormFieldFactory.createListField(name, dispName, getStatusListTypeModel(),true,isNotNull);
		}
		else if(name.equals(UserModel.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}
		return formField;
	}

	private List<ListTypeModel<Integer>> getStatusListTypeModel(){
		List<ListTypeModel<Integer>> statusValues = new ArrayList<ListTypeModel<Integer>>();
		Integer code = UserStatus.STATUS_NORMAL;
		ListTypeModel<Integer> ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(UserModel.STATUS_NORMAL_NAME));
		statusValues.add(ltModel);
		code = UserStatus.STATUS_FORBID;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(UserModel.STATUS_FORBID_NAME));
		statusValues.add(ltModel);
		code = UserStatus.STATUS_DELETE;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(UserModel.STATUS_DELETE_NAME));
		statusValues.add(ltModel);
		return statusValues;
	}

	public List<ColumnConfig> getColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);
		
		ColumnConfig column = new ColumnConfig();
		String name = UserModel.CODE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(150);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = UserModel.NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(150);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = UserModel.START_DATE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(300); 
	    column.setDateTimeFormat(dateTimeFormat);
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = UserModel.END_DATE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(300); 
	    column.setDateTimeFormat(dateTimeFormat);
	    configs.add(column);
		
	    Object[] statusCodes = {UserStatus.STATUS_NORMAL,UserStatus.STATUS_FORBID,UserStatus.STATUS_DELETE};
		String s1 = getFieldDisplayName(UserModel.STATUS_NORMAL_NAME);
		String s2 = getFieldDisplayName(UserModel.STATUS_FORBID_NAME);
		String s3 = getFieldDisplayName(UserModel.STATUS_DELETE_NAME);
		String[] statusNames = {s1,s2,s3};
	    GridCellRenderer<UserModel> statusRenderer = MyGridHelper.createListCellRender(UserModel.STATUS, statusCodes, statusNames);
		column = new ColumnConfig();
		name = UserModel.STATUS;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(300); 
	    column.setRenderer(statusRenderer);
	    configs.add(column);
	    
		return configs;
	}
	
	public String getFieldDisplayName(String code) {
		return UserFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

	@Override
	public UserModel createModelInstance() {
		UserModel model = new UserModel();
		return null;
	}

}
